package cn.itcast.controller;


import cn.hutool.core.collection.CollUtil;
import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;
    @Reference(version = "1.0.0")
    private RoleService roleService;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping
    public Result queryPermissionList(QueryPageBean queryPageBean) {
        try {
            log.debug("接收的参数:{}", queryPageBean);
            PageResult pageResult = permissionService.queryPermissionList(queryPageBean);
            log.debug("查询出的数据:{}", pageResult);
            if (null != pageResult) {
                return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, pageResult);
            }
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL, e);
        }

    }

    /**
     * 新增权限
     */
    @PostMapping
    public Result addPermission(@RequestBody Map<String, String> param) {
        try {
            log.debug("新增接收的参数:{}", param);
            String name = param.get("name");
            String keyword = param.get("keyword");
            String description = param.get("description");

            Boolean result = permissionService.addPermission(name, keyword, description);
            if (result) {
                return new Result(result, MessageConstant.ADD_PERMISSION_SUCCESS);
            }
            return new Result(result, MessageConstant.ADD_PERMISSION_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL, e);
        }
    }


    /**
     * 回显用户信息
     *
     * @return
     */
    @GetMapping("/{id}")
    public Result echoPermission(@PathVariable("id") String pid) {
        try {
            log.debug("修改接收的参数:{}", pid);

            Permission permission = permissionService.echoPermission(pid);
            if (null != permission) {
                return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS, permission);
            }
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL, e);
        }

    }

    /**
     * 修改
     *
     * @param permission
     * @return
     */
    @PutMapping
    public Result updatePermission(@RequestBody Permission permission) {
        try {
            log.debug("修改的数据:{}", permission);
            Boolean result = permissionService.updatePermission(permission);
            if (result) {
                return new Result(result, MessageConstant.EDIT_PERMISSION_SUCCESS);
            }
            return new Result(result, MessageConstant.EDIT_PERMISSION_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL, e);
        }

    }

    /**
     * 删除
     *
     * @param pid
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deletePermission(@PathVariable("id") String pid) {
        try {
            log.debug("删除的id:{}", pid);
            Boolean result = permissionService.deletePermission(pid);
            if (result) {
                return new Result(result, MessageConstant.DELETE_PERMISSION_SUCCESS);
            }
            return new Result(result, MessageConstant.DELETE_PERMISSION_FAIL);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL, e);
        }
    }


    /**
     * 根据MenuId查询权限信息
     *
     * @return
     */
    @GetMapping("all")
    public Result queryPermissioList() {
        List<Permission> permissionList = null;
        try {
            permissionList = permissionService.queryPermissioList();

            if (CollUtil.isNotEmpty(permissionList)) {
                return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissionList);
            }
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL, e);
        }

    }


    //菜单权限查询
    @GetMapping("/menu/association")
    public Result findMenuAssociationPermission(Integer[] menuIds) {

        log.info("进入菜单关联权限查询==============");
        try {
            Set<Permission> permissions = roleService.findMenuAssociationPermission(menuIds);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS, permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

}
